package com.jollychic.www.roomdatebasedemo.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatEditText;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jollychic.www.roomdatebasedemo.DaoManager;
import com.jollychic.www.roomdatebasedemo.R;
import com.jollychic.www.roomdatebasedemo.dao.IStoreDao;
import com.jollychic.www.roomdatebasedemo.model.StoreEntity;

import io.reactivex.Completable;
import io.reactivex.schedulers.Schedulers;

/**
 * desc: 删除数据 <br/>
 * author: 李超杰 <br/>
 * time: 2018/4/22 17:42 <br/>
 * since: V 6.1X <br/>
 */

public class FragmentDelete extends Fragment {

    private AppCompatEditText etDeleteId;
    private AppCompatButton btDelete;
    private AppCompatButton btDeleteAll;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View view = LayoutInflater.from(getContext())
                .inflate(R.layout.fragment_delete, container, false);
        etDeleteId = view.findViewById(R.id.et_delete_id);
        btDelete = view.findViewById(R.id.bt_delete);
        btDeleteAll = view.findViewById(R.id.bt_delete_all);
        loadListener();
        return view;
    }

    private void loadListener() {
        btDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                delete();
            }
        });
        btDeleteAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                deleteAll();
            }
        });
    }


    private void delete() {
        if (TextUtils.isEmpty(getDeleteId())) {
            return;
        }
        Completable
                .fromRunnable(new Runnable() {
                    @Override
                    public void run() {
                        final StoreEntity storeEntity = new StoreEntity();
                        storeEntity.setStoreId(getDeleteId());
                        DaoManager.getInstance().getIStoreDao().delete(storeEntity);
                    }
                })
                .subscribeOn(Schedulers.io())
                .subscribe();
    }

    /**
     * 删除所有店铺数据
     */
    private void deleteAll() {
        Completable
                .fromRunnable(new Runnable() {
                    @Override
                    public void run() {
                        final IStoreDao dao = DaoManager.getInstance().getIStoreDao();
                        dao.delete(dao.getStoresList());
                    }
                })
                .subscribeOn(Schedulers.io())
                .subscribe();
    }


    private String getDeleteId() {
        return etDeleteId.getText().toString().trim();
    }
}
